package com.tanhua.server.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName :  MyResponseBodyAdvice
 * @Author : Tenebrous
 * @Date: 2020/12/12 19:22
 * @Description : 替换Request对象
 */
@Component
public class RequestReplaceFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 判断request请求是不是MyServletRequestWrapper类型
        if (!(request instanceof MyServletRequestWrapper)) {
            // 不是的话，包装下request
            request = new MyServletRequestWrapper(request);
        }
        // 否则放行
        filterChain.doFilter(request, response);
    }
}